package com.jubao.ejb.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.jubao.ejb.dao.UserWebsiteDaoLocal;
import com.jubao.ejb.dao.UserWebsiteDaoRemote;
import com.jubao.ejb.dao.util.DaoConstant;
import com.jubao.ejb.dao.util.DaoUtil;
import com.jubao.ejb.entity.ProductPrice;
import com.jubao.ejb.entity.UserWebsite;

@Stateless
@LocalBinding(jndiBinding = DaoConstant.UserWebsiteDaoLocalJndiName)
@RemoteBinding(jndiBinding = DaoConstant.UserWebsiteDaoRemoteJndiName)
public class UserWebsiteDaoImpl implements UserWebsiteDaoLocal, UserWebsiteDaoRemote {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1367320897388431253L;


	static Logger logger = Logger.getLogger(UserWebsiteDaoImpl.class);

	
	@PersistenceContext(unitName = "default")
	protected EntityManager em;

	public void setEm(EntityManager em) {
		this.em = em;
	}

	public UserWebsite find(long sysId) {
		// TODO Auto-generated method stub
		return null;
	}

	public UserWebsite find(long userSysId, int relationId, long websiteSysId) {
		if (userSysId > -1 && relationId > -1 && websiteSysId > -1) {
			List<UserWebsite> pps = this.getUserWebsiteList(userSysId, relationId, websiteSysId, 0, 1);
			if (pps != null && pps.size() > 0) {
				return pps.get(0);
			}
		}

		return null;
	}

	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public UserWebsite save(UserWebsite productPrice) {
		return em.merge(productPrice);
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.dao.UserWebsiteDao#getUserWebsiteCount(long, int, long)
	 */
	public long getUserWebsiteCount(long userSysId, int relationId, long websiteSysId) {
		Query q = this.composeGetUserWebsiteQuery(userSysId, relationId, websiteSysId, false);
		Long result = (Long) q.getSingleResult();
		return result;
	}

	/* (non-Javadoc)
	 * @see com.jubao.ejb.dao.UserWebsiteDao#getUserWebsiteList(long, int, long, int, int)
	 */
	public List<UserWebsite> getUserWebsiteList(long userSysId, int relationId, long websiteSysId, int pgno, int pgsize) {
		List<ProductPrice> result = new ArrayList<ProductPrice>(0);
		Query q = this.composeGetUserWebsiteQuery(userSysId, relationId, websiteSysId, true);
		//.setHint("org.hibernate.cacheRegion",	"/entity/Category/RelatedCategory/List")
		List rs = DaoUtil.getResultList(q, pgno, pgsize);
		return rs;
	}

	
	private Query composeGetUserWebsiteQuery( long userSysId, int relationId, long websiteSysId, boolean isGetList) {
		String ent = isGetList ? "pp" : "count(pp)";

		String eql = "select " + ent + " from  UserWebsite pp";
		String whereand = "";
		if (userSysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  pp.user.sysId=:userSysId ";
		}
		if (relationId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  pp.relationId=:relationId ";
		}
		if (websiteSysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  pp.website.sysId=:websiteSysId ";
		}
		
		logger.info("eql=[" + eql + "]");

		Query q = em.createQuery(eql);
		if (userSysId > -1) {
			q.setParameter("userSysId", userSysId);
		}
		if (relationId > -1) {
			q.setParameter("relationId", relationId);
		}
		if (websiteSysId > -1) {
			q.setParameter("websiteSysId", websiteSysId);
		}
		return q;
	}

}
